package org.redis.cluster.auth;

import org.apache.commons.lang3.StringUtils;
import org.redis.cluster.RedisCluster;
import org.redis.cluster.transport.session.Session;

public class AuthHelper {

	/**
	 * @param session
	 * @param password
	 * @return
	 */
	public static boolean isAuth(Session session,String password) {
		String authPassword=session.getContext().get(AuthConstants.PASSWORD_CONTEXT);
		if(StringUtils.isBlank(authPassword)) {
			return false;
		}
		
		return authPassword.equals(password);
	}
	
	/**
	 * @param session
	 * @param password
	 * @return
	 */
	public static boolean setAuth(Session session,String password) {
		return session.getContext().put(AuthConstants.PASSWORD_CONTEXT, password);
	}
	
	/**
	 * @param redisCluster
	 * @return
	 */
	public static String getPassword(RedisCluster redisCluster) {
		return redisCluster.getConfigManager().getSystemConfig().get(AuthConstants.PASSWORD_CONFIG).getValue(String.class,AuthConstants.DEFAULT_PASSWORD);
	}
}